package com.scy.hospitalmanager.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.scy.hospitalmanager.dao.nursedao.NurseLoginDao;
import com.scy.hospitalmanager.pojo.Nurse;
import com.scy.hospitalmanager.pojo.NurseLogin;
import com.scy.hospitalmanager.service.impl.NurseMsgServiceImpl;
import com.scy.hospitalmanager.service.impl.NurseTimeTableServiceImpl;
import com.scy.hospitalmanager.until.GetPageNum;
import com.scy.hospitalmanager.until.MD5;
import com.scy.hospitalmanager.until.ResponseMap;
import com.scy.hospitalmanager.until.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

/**
 * @Author Song_can_yu
 * @ClassName NurseMsgController
 * @Date 2021-08-18-17:48
 * @PackageName com.scy.hospitalmanager.controller
 * @Description
 */
@Controller
public class NurseMsgController {
    @Autowired
    private NurseMsgServiceImpl nurseMsgService;
    @Autowired
    private NurseTimeTableServiceImpl nurseTimeTableService;
    @Autowired
    private Gson gson;
    @Autowired
    private NurseLoginDao nurseLoginDao;
    @PostMapping("/getNurseMsg")
    @ResponseBody
    public ResponseMap getNurseMsg(@RequestBody GetPageNum getPageNum, @RequestHeader("token") String token) {
        getPageNum.setPageSize(10);
        if (getPageNum.getPageNo() == 0) {
            getPageNum.setPageNo(1);
        }
        PageInfo<Nurse> nursePageInfo = nurseMsgService.getAllNurses(getPageNum);
//        System.out.println(TokenUtil.parsePayloadData(token));
        Map map = TokenUtil.parsePayloadData(token);
//        System.out.println(map.get("nurseLogin"));
        return ResponseMap.OK(nursePageInfo);
    }

        @PostMapping("/getNurseById")
    @ResponseBody
    public ResponseMap getNurseById(@RequestBody Nurse nurse) {
        if (nurse == null) {
            return ResponseMap.NO();
        } else {
            return ResponseMap.OK(nurseMsgService.getNurseById(nurse));
        }
    }

    @PostMapping("/updateNurseMsg")
    @ResponseBody
    public ResponseMap updateNurseMsg(@RequestBody Nurse nurse) {
        if (nurse == null) {
            return ResponseMap.NO();
        } else {
            int status = nurseMsgService.updateNurseMsgById(nurse);
            return ResponseMap.OK(status);
        }
    }

    @PostMapping("/deleteNurseMsgById")
    @ResponseBody
    public ResponseMap deleteNurseMsgById(@RequestBody Nurse nurse) {
        if (nurse == null) {
            return ResponseMap.NO();
        }
        return ResponseMap.OK(nurseMsgService.deleteNurseMsgById(nurse));
    }

    @GetMapping("/getAllNurseTimeTable")
    @ResponseBody
    public ResponseMap getAllNurseTimeTable() {
        if (nurseTimeTableService.getAllNurseTimeTable() == null) {
            return ResponseMap.NO();
        } else {
            return ResponseMap.OK(nurseTimeTableService.getAllNurseTimeTable());
        }
    }

    @GetMapping("/getAllDep")
    @ResponseBody
    public ResponseMap getAllDep() {
        return ResponseMap.OK(nurseMsgService.getAllDep());
    }

    @GetMapping("/getAllPost")
    @ResponseBody
    public ResponseMap getAllPost() {
        return ResponseMap.OK(nurseMsgService.getAllPost());
    }

    @GetMapping("/getAllQual")
    @ResponseBody
    public ResponseMap getAllQual() {
        return ResponseMap.OK(nurseMsgService.getAllQual());
    }

    @PostMapping("/insertNurse")
    @ResponseBody
    public ResponseMap insertNurse(@RequestBody Nurse nurse) {
        int status = nurseMsgService.insertNurse(nurse);
        if (status == 0) {
            return ResponseMap.NO();
        } else {
            return ResponseMap.OK(status);
        }
    }
    @PostMapping("/gerNurseTransition")
    @ResponseBody
    public ResponseMap getNurseTransition(){
        return ResponseMap.OK(nurseMsgService.getAllNurseTransition());
    }
    @PostMapping("/getCurrentMsg")
    @ResponseBody
    public ResponseMap getCurrentMsg(@RequestHeader("token")String token) throws ParseException {
        Map<String,Object>nurseMap= TokenUtil.parsePayloadData(token);
        String str = gson.toJson(nurseMap.get("nurseLogin"));
        String str2 = gson.toJson(nurseMap.get("currentTime"));
        NurseLogin nurseLogin = gson.fromJson(str,NurseLogin.class);
        Nurse nurse = new Nurse();
        nurse.setNurId((int) nurseLogin.getNlNurseId());
        nurseLogin.setNurse(nurseMsgService.getNurseById(nurse));
        nurseLogin.setCurrentDate(str2.replace("\"", ""));
        NurseLogin nurseLogin1 = nurseLoginDao.getByNurseLoginId(nurseLogin);
        System.out.println(nurseLogin1);
        nurseLogin1.setCurrentDate(nurseLogin.getCurrentDate());
        nurseLogin1.setNurse(nurseLogin.getNurse());
        return ResponseMap.OK(nurseLogin1);

    }
}